import { appRef } from "./app.ref";
import { AssertsToken } from "./@type";

export type GlobalInfo = {
  token?: string;
  assetToken?: AssertsToken;
}

export type ConfigInfo = {
  relationUrl: string;
}

class AppContext {
  private static _ins: AppContext = new AppContext();
  private _global = <GlobalInfo>{};
  private _config = <ConfigInfo>{}

  constructor() { }

  public loginOut(): void {
    this._global = {} as GlobalInfo;
    appRef.loginOut();
    console.log(AppContext._ins, appRef.user);
    localStorage.setItem("token", '');
  }

  public static get ins(): AppContext {
    return this._ins ? this._ins : this._ins = new AppContext();
  }

  public get global(): GlobalInfo {
    if (!this._global.token) { this._global.token = localStorage.getItem("token") }
    return this._global;
  }
  public setGlobal({
    token = this._global.token,
    assetToken = this._global.assetToken,
  }: {
    token?: string,
    assetToken?: AssertsToken,
  } = {}) {
    if (token) { this._global.token = token; localStorage.setItem("token", token) }
    this._global.assetToken = assetToken;
  }

  /** 静态配置文件 */
  public get config(): ConfigInfo {
    return this._config
  }
  public setConfig(info: ConfigInfo): void {
    this._config = info;
  }

}
/** 全局 非响应式数据 管理 */
export const appContext: AppContext = AppContext.ins;